package com.xing.serviceedu.controller;


import com.xing.commonutils.R;
import com.xing.serviceedu.client.OssClient;
import com.xing.serviceedu.entity.EduVideo;
import com.xing.serviceedu.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author xingfuao
 * @since 2022-01-09
 */
@RestController
@RequestMapping("/serviceedu/video")
@CrossOrigin //跨域的注解
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Resource
    private OssClient ossClient;


    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    // TODO 后面这个方法是需要完善的:删除小节的时候，
    //  同时把里面的视频也要删除
    @DeleteMapping("deleteVideo/{id}")
    public R deleteVideo(@PathVariable("id") String id, MultipartFile file){
        //根据小节id获取视频id,调用方法实现视频删除
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节里面是否有视频id

        if(!StringUtils.isEmpty(videoSourceId)){
            //根据视频id,感觉视频id,远程调用实现视频删除
//            ossClient.uploadOssFile(file);
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }
    @PostMapping(name = "a",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R Video(MultipartFile file){
        //根据小节id获取视频id,调用方法实现视频删除
//        EduVideo eduVideo = videoService.getById(1);
//        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节里面是否有视频id

//        if(!StringUtils.isEmpty(videoSourceId)){
            //根据视频id,感觉视频id,远程调用实现视频删除
            ossClient.uploadOssFile(file);
//        }
        //删除小节
//        videoService.removeById(1);
        return R.ok();
    }

    @PostMapping("/a")
    public R a(){
        R ab = ossClient.a();
        return R.ok().data("a",ab);
    }
}

